package com.ruiyangwang.ListenBook.servlet.user;
import com.ruiyangwang.ListenBook.service.UserService;
import com.ruiyangwang.ListenBook.viewObject.UserV0;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author syzygy
 * @date 2022/07/09 14:57
 **/
@WebServlet("/studio/user/register.do")
public class RegisterDoServlet extends HttpServlet {
    //表示过程的对象一般为为单例，定义为属性
    private final UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取用户名和密码
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //TODO:输入合法性校验
        //完成注册（密码加密加insert表，放到service处理）
        //用户放到session中，属于表现对象，所以放到view_object中
        UserV0 userV0 = userService.register(username,password);
        //3.注册后直接登录
        HttpSession session = req.getSession();
        session.setAttribute("currentUser",userV0);
        //4.重定向首页
        resp.sendRedirect("/studio/");

    }
}